<?php
/**
 * Created by PhpStorm.
 * User: chniccs
 * Date: 2019-09-07
 * Time: 8:53
 */

namespace app\admin\controller;


use app\model\AdminSiteConfig;
use app\util\ReturnCode;

class SiteView extends Base
{
    public function index()
    {

    }
    public function getSiteView(){
        try{
            $model = AdminSiteConfig::get(['tid'=>$this->getUserTid()]);
            if($model){
                $data = $model->getData();
                return $this->buildSuccess($data);

            }else{
                //没有数据的话，也返回成功，让前台创建
                return $this->buildSuccess(null);
            }
        }catch (\Exception $e){
            return $this->buildSuccess(null);
        }
    }

    /**
     * 保存站点首页布局数据
     */
    public function saveSiteView()
    {
        $jsondata = $this->request->param('jsondata');
        $model = AdminSiteConfig::get(["tid"=>$this->getUserTid()]);
        if ($model) {
            //如果存在，就修改
            $model->setAttr('view_config',$jsondata);
            $affected = $model->save();
            if(!empty($affected)){
                return $this->buildSuccess(null, '保存成功');
            }else{
                return $this->buildFailed(ReturnCode::FAILED, '保存失败');
            }
        } else {
            //不存在就添加
            $data = [];
            $data = $this->addTid($data);
            $data['view_config'] = $jsondata;
            $model = AdminSiteConfig::create($data);
            //获取新增数据的自增主键id
            if ($model) {
                return $this->buildSuccess(null, '保存成功');
            } else {
                return $this->buildFailed(ReturnCode::FAILED, '保存失败');
            }
        }
    }

}